﻿#if WINDOWS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.IO;

namespace FITClub.Episode1.Levels
{

    /// <summary>
    /// UI Property Grig Editor for Content Path
    /// </summary>
    public class ContentPathEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            //IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            if (context.PropertyDescriptor.PropertyType == typeof(string))
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "Select content file";
                    ofd.InitialDirectory = Path.GetFullPath("Content");
                    ofd.Filter = "XNA Content (*.xnb)|*.xnb|All Files|(*.*)";
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {

                        return StripContentFolderPath(ofd.FileName);
                    }
                }
            }
            return value;
        }


        public static string StripContentFolderPath(string filename)
        {
            int i = filename.LastIndexOf("Content");
            if (i >= 0)
            {
                filename = filename.Substring(i+ 8);
            }
            filename.TrimStart('\\');
            i = filename.LastIndexOf(".xnb");
            if (i >= 0)
            {
                filename = filename.Substring(0, i);
            }
            return filename;
        }
    }
}
#endif
